<?php

namespace App\Imports;

use App\Http\Models\Business\InvoiceDetails;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class SpecialOrderImport implements ToCollection, WithHeadingRow
{
    use BaseImportTrait;

    private $required_field = ['销售订单号', '是否特价'];

    public function collection(Collection $rows)
    {
        foreach ($rows as $key => $row) {
            $row = $row->toArray();

            if ($key === 0) {
                $this->verifyField($row);
                $this->prepareBaseData();
            }

            $this->verifyNotNull($row, $key + 2);
            $this->verifyValueExist($row, $key + 2);

            $this->saveData($row);
        }
    }


    //准备基础数据，用来验证
    private function prepareBaseData()
    {
    }


    //验证字段值存在于数据库基础表中
    private function verifyValueExist(array $row, int $index)
    {
    }


    //保存数据
    private function saveData(array $row)
    {
        $is_special = 0;
        if ($row['是否特价'] == '是') {
            $is_special = 1;
        } elseif ($row['是否特价'] == '否') {
            $is_special = 0;
        }
        InvoiceDetails::where(['sales_order_no' => $row['销售订单号']])->update(['is_special' => $is_special]);
    }
}
